const BaseIpcHandler = require('../BaseIpcHandler');
const fs = require('fs');

class ReadFileHandler extends BaseIpcHandler {
    async handle(event, params) {
        // 解析参数
        // console.log('>> ReadFileHandler params = ', params);
        const filePath = params.file_path;

        // 业务逻辑
        // let rootDir = getUserDataPath('labelapp');
        let result = null;
        try {
            const fileContents = fs.readFileSync(filePath, 'utf-8');
            result = { code: 0, msg: fileContents };
        } catch (error) {
            result = { code: -1, msg: error.message };
        }

        // 返回结果
        if (event.from === 'localserver') {
            event.resp.send(result);
        } else {
            return result;
        }
    }
}

module.exports = ReadFileHandler;
